package com.houseliker.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.houseliker.Constants;
import com.houseliker.R;
import com.houseliker.view.IGifViewForSplash;

/**
 * Created by sunrx on 2015/12/14.
 */
public class MainSplashActivity extends BaseActivity {
    private String currentVersion;

    private IGifViewForSplash gifView;

    private TextView tv_jump;

    private SharedPreferences sp;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            Intent intent = new Intent(mContext, MainActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setView(R.layout.mainsplash, TYPE_NORMAL);

        sp = getSharedPreferences(Constants.SP_LAUNCHER, MODE_PRIVATE);

        gifView = (IGifViewForSplash) findViewById(R.id.gifView);
        tv_jump = (TextView) findViewById(R.id.tv_jump);
        tv_jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean(Constants.KEY_ENABLE, false);
                editor.commit();
            }
        });

        fillDatas();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void fillDatas() {

        gifView.setDrawable();

        boolean enable = sp.getBoolean(Constants.KEY_ENABLE, true);
        if (enable) {
            mHandler.sendEmptyMessageDelayed(0, 3000);
        } else {
            mHandler.obtainMessage().sendToTarget();
        }
    }

}
